/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui1;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author zheng
 */
public class Jtable implements GoodsDao{
    
    private JTable jTable;

    public Jtable(JTable jTable) {
        this.jTable = jTable;
    }
    
    
    

    public boolean save(Goods good) {
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        String[] strs = convertEmployeeToStringArr(good);      
        model.addRow(strs);
        return true;
    }

    private String[] convertEmployeeToStringArr(Goods good) {
            String[] clothes=new String[3];
            clothes[0]=good.getName();
            clothes[1]=Double.toString(good.getPrice());
            clothes[2]=Integer.toString(good.getNum());
       return clothes;
    }

    @Override
    public boolean deleteByIndex(int index) {
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();        
        model.removeRow(index);
        return true;
    }
    public void clear(){
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();        
        model.setRowCount(0); 
    }
}
